Day 12 - Regular expressions - Single characters

43

$ head examples.txt | sed -r s,".g","--",g

d--

cat

elephant

ostrich

D-- the D--

beholder

dryad

d--

Police 101

aardvark

The previous command searches for any character followed by a lowercase g (.g) and replaces it

with a double dash. It obviously doesn’t make sense to have regular expressions in the replacement

part of the command, as regexes are used to search.

Regular expressions in sed are often used to delete unwanted parts of a line. Let’s say that I want to

print the first 5 lines of the file simple.log removing everything after the GET part of the line

$ head -n 5 simple.log | sed -r s,"GET.*","GET",

83.149.9.216 [17/May/2015:10:05:03] GET

83.149.9.216 [17/May/2015:10:05:43] GET

83.149.9.216 [17/May/2015:10:05:47] GET

83.149.9.216 [17/May/2015:10:05:12] GET

83.149.9.216 [17/May/2015:10:05:07] GET

I used the .* patter that matches all to remove the part od the line that I didn’t want in the output.

As you can see I need to repeat GET in order to preserve it. We will learn later in the book a technique

to avoid this repetition.

Exercises

Exercise 12.01

Match “dog”, “Dog”, and “hog” into examples.txt

Go to solution